from pylons import config
from sqlalchemy import *
from sqlalchemy.orm import relation
from gigboard.model import metadata, mapper, DBSession

from datetime import datetime, timedelta

# Normal tables may be defined and mapped at module level.

# Define the Gig class first so it's available for the expiry_date
# column's default value
class Gig(object):
    def __init__(self, **kw):
        """automatically mapping attributes"""
        for key, value in kw.iteritems():
            setattr(self, key, value)
            
    @classmethod
    def _get_new_expiry(cls):
        """Return an expiry date 28 days from now"""
        return datetime.utcnow() + timedelta(days=28)
        
    def extend_expiry(self):
        """Extend the expiry of this Gig"""
        self.expiry_date = self._get_new_expiry()
        DBSession.flush()

gig_table = Table('gigs', metadata,
    Column('id', Integer, primary_key=True),
    Column('company', Unicode(255), nullable=False),
    Column('company_confidential', Boolean, default=False),
    Column('url', Unicode(255)),
    Column('country_code', String(5)),
    Column('location', Unicode(255)),
    Column('geo_x', Float(precision=20)),
    Column('geo_y', Float(precision=20)),
    Column('anywhere', Boolean, default=False),
    Column('job_title', Unicode(255), nullable=False),
    Column('description', UnicodeText, nullable=False),
    Column('app_destination', Unicode(255), nullable=False),
    Column('creator_id', Integer, ForeignKey('user.id'), nullable=False),
    Column('creation_date', DateTime, default=datetime.utcnow),
    Column('is_active', Boolean, default=True),
    Column('expiry_date', DateTime, default=Gig._get_new_expiry)
)

mapper(Gig, gig_table)

